package com.zsr.system.service.qw.impl;

import com.alibaba.fastjson2.JSONObject;
import com.zsr.system.service.qw.IWeChatMessageService;
import com.zsr.system.service.qw.IWechatService;
import lombok.extern.slf4j.Slf4j;
import org.dom4j.Element;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 荣
 * @date 2025/3/27 10:58
 */
@Service
@Slf4j
public class WeChatMessageServiceImpl implements IWeChatMessageService {

    @Resource
    private IWechatService wechatService;
    @Override
    public void handleChat(Element root) {
        String changeType = root.elementText("ChangeType");
        String chatId = root.elementText("ChatId"); // 群ID
        if ("create".equals(changeType)) {
            log.info("群创建");
            JSONObject json = wechatService.getChatInfo(wechatService.getAccessToken(1), chatId, 0).getJSONObject("group_chat");

        }

        if ("update".equals(changeType)) {
            log.info("群变更");
//            变更详情。目前有以下几种：
//            add_member : 成员入群
//            del_member : 成员退群
//            change_owner : 群主变更
//            change_name : 群名变更
//            change_notice : 群公告变更
            String updateDetail = root.elementText("UpdateDetail");
            if ("add_member".equals(updateDetail)) {

                // 提取 MemChangeList 中的数组，获取新增的客户ID
                Element memChangeListElement = root.element("MemChangeList");
                List<Element> items = memChangeListElement.elements("Item");
                List<String> userIds = new ArrayList<>();
                for (Element item : items) {
                    userIds.add(item.getText());
                }
                if (userIds.isEmpty()) {
                    log.info("群里没有新增的外部客户，无需打标签");
                    return;
                }

                for (String externalUserId : userIds) {
                    JSONObject resp = wechatService.getCustomerInfo(wechatService.getAccessToken(1), externalUserId);
                    log.info("获取到客户详情：{}", resp);
                    if (resp.getInteger("errcode") == 0) {
                        // 自动打标签，记录日志

                    }
                }
            }
        }

        if ("dismiss".equals(changeType)) {
            log.info("群解散");

        }
    }

    @Override
    @Async
    public void handleTag(Element root) {
        String tagType = root.elementText("TagType"); // 标签类型
        String changeType = root.elementText("ChangeType"); // 事件类型
        String tagId = root.elementText("Id"); // 标签类型

        if ("delete".equals(changeType)) {
            if ("tag_group".equals(tagType)) {
                log.info("目前标签助手未保存标签组信息，删除标签组");
//                if ("etPXtjBgAA_JpkcHdUR3slha91Mc42Vw".equals(tagId)) {
//                    qwSchoolChatTagService.remove(new LambdaQueryWrapper<>());
//                }
            }
//            LambdaQueryWrapper<QwSchoolChatTag> queryWrapper = new LambdaQueryWrapper<>();
//            queryWrapper.eq(QwSchoolChatTag::getTagId, tagId);
//            qwSchoolChatTagService.removeById(queryWrapper); // 删除该标签规则

        }
    }
}
